package com.ssd.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssd.entity.VendingMachineEntity;
import org.apache.ibatis.annotations.*;

import java.util.Map;

@Mapper
public interface VMDao extends BaseMapper<VendingMachineEntity> {

    @Select("<script>" +
            "SELECT * FROM tb_vending_machine t1 " +
            "<where> " +
            "<if test='innerCode != null and innerCode != \"\"'> " +
            "AND t1.inner_code like CONCAT('%', #{innerCode}, '%')" +
            "</if> " +
            "</where> " +
            "</script>")
    @Results({
            @Result(property="id", column="id"),
            @Result(property="vmType", column="vm_type"),
            @Result(property="innerCode", column="inner_code"),
            @Result(property="nodeId", column="node_id"),
            @Result(property="vmStatus", column="vm_status"),
            @Result(property="lastSupplyTime", column="last_supply_time"),
            @Result(property="cityCode", column="city_code"),
            @Result(property="areaId", column="area_id"),
            @Result(property="createUserId", column="create_user_id"),
            @Result(property="createUserName", column="create_user_name"),
            @Result(property="clientId", column="client_id"),
            @Result(property="longitudes", column="longitudes"),
            @Result(property="latitude", column="latitude"),
            @Result(property="ownerId", column="owner_id"),
            @Result(property="ownerName", column="owner_name"),
            @Result(property="businessId", column="business_id"),
            @Result(property="regionId", column="region_id"),
            @Result(property="partnerId", column="partner_id"),
            @Result(property="policyId", column="policy_id"),
            @Result(property="runningStatus", column="running_status"),
    })
    Page<VendingMachineEntity> selectVM(Page<?> page, String innerCode);

    /**
     * 新增售货机信息
     *
     * @param vm 售货机信息
     * @return 结果
     */
    @Insert("INSERT INTO tb_vending_machine (vm_type, inner_code, node_id, vm_status, last_supply_time, city_code, area_id, create_user_id, " +
            "create_user_name, business_id, region_id, owner_id, owner_name, client_id, longitudes, latitude,partner_id,policy_id,running_status) " +
            "VALUES (#{vmType}, #{innerCode}, #{nodeId}, #{vmStatus}, #{lastSupplyTime}, #{cityCode}, #{areaId}, #{createUserId}, " +
            "#{createUserName}, #{businessId}, #{regionId}, #{ownerId}, #{ownerName}, #{clientId}, #{longitudes}, #{latitude}, #{runningStatus}," +
            "#{partnerId},#{policyId})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertVM(VendingMachineEntity vm);

    /**
     * 修改售货机信息
     *
     * @param vm 售货机信息
     * @return 结果
     */
    @Update("UPDATE tb_vending_machine SET vm_type=#{vmType}, inner_code=#{innerCode}, node_id=#{nodeId}, vm_status=#{vmStatus}, " +
            "last_supply_time=#{lastSupplyTime}, city_code=#{cityCode}, area_id=#{areaId}, create_user_id=#{createUserId}, " +
            "create_user_name=#{createUserName}, business_id=#{businessId}, region_id=#{regionId}, owner_id=#{ownerId}, " +
            "owner_name=#{ownerName}, client_id=#{clientId}, longitudes=#{longitudes}, latitude=#{latitude} ,policy_id=#{policyId}" +
            ",partner_id=#{partnerId}" +
            "WHERE id=#{id}")
    int updateVM(VendingMachineEntity vm);

    /**
     * 删除售货机信息
     *
     * @param vmId 售货机主键
     * @return 结果
     */
    @Delete("DELETE FROM tb_vending_machine WHERE id = #{vmId}")
    int deleteVMByVMId(Long vmId);

    /**
     * 批量删除售货机信息
     *
     * @param vmIds 需要删除的售货机主键集合
     * @return 结果
     */
    @Delete("<script>" +
            "DELETE FROM tb_vending_machine" +
            "<where>" +
            "id IN" +
            "<foreach item='id' index='index' collection='array' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</where>" +
            "</script>")
    int deleteVMByVMIds(Long[] vmIds);

    /**
     * 根据售货机ID查询售货机详情
     *
     * @param vmId 售货机主键
     * @return 售货机详情
     */
    @Select("SELECT * FROM tb_vending_machine WHERE id = #{vmId}")
    VendingMachineEntity selectVMByVMId(Long vmId);
}